package com.xidian.zhiancloud.service.dangerWorkTicket;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xidian.zhiancloud.dao.syset.SysPositionMapper;
import com.xidian.zhiancloud.dao.workTicket.WorkTicketMapper;
import com.xidian.zhiancloud.dao.workTicket.WorkTicketTemplateMapper;
import com.xidian.zhiancloud.model.syset.SysPosition;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicket;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicketTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

@Service
public class WorkTicketStatisticsService {

    @Autowired
    private WorkTicketMapper workTicketMapper;

    @Autowired
    private WorkTicketTemplateMapper workTicketTemplateMapper;

    @Autowired
    private SysPositionMapper sysPositionMapper;

    public Map<String, Integer> statisticsByPositionId(Long companyId, Date startTime, Date endTime) {
        Map<String, Integer> ans = new HashMap<>();
        List<WorkTicket> list = null;


        LambdaQueryWrapper<WorkTicket> query = Wrappers.<WorkTicket>lambdaQuery();
        query.eq(companyId != null, WorkTicket::getCompanyId, companyId)
                .between(startTime != null && endTime != null, WorkTicket::getCreateTime, startTime, endTime);
        list = workTicketMapper.selectList(query);

        if (list == null) {
            return ans;
        } else {
            // 遍历作业票的类型
            for (WorkTicket workTicket : list) {
                String positionName = "该位置已删除";
                Long positionId = workTicket.getPositionId();
                SysPosition sysPosition = sysPositionMapper.selectById(positionId);
                if (sysPosition != null) {
                    positionName = sysPosition.getName();
                }
                // 如果出现新的作业票类型，保存key，并将value设置为1
                if (!ans.containsKey(positionName)) {
                    ans.put(positionName, 1);
                } else {
                    // 将原来key的value值加1
                    ans.put(positionName, ans.get(positionName) + 1);
                }
            }
        }
        return ans;
    }

    /**
     * 作用：统计一定时间段内，各种作业票类型的数量
     * 返回值类型：map，参数一为作业票类型，参数二为对应作业票的数量
     * 函数逻辑：票类型 = ticket.type，Byte的Integer对象 + 1
     *
     * TODO：有个bug，t_work_ticket的ticket_type要在t_work_ticket_template中查找name，如果作业票模板被删除，则作业票找不到对应的模板id，拿不到name属性，就会报错
     */
    public Map<String, Integer> statisticsByType(Long companyId, Date startTime, Date endTime) {
        Map<String, Integer> ans = new HashMap<>();
        List<WorkTicket> list = null;

        LambdaQueryWrapper<WorkTicket> query = Wrappers.<WorkTicket>lambdaQuery();
        query.eq(companyId != null, WorkTicket::getCompanyId, companyId).
                between(startTime != null && endTime != null, WorkTicket::getCreateTime, startTime, endTime);
        list = workTicketMapper.selectList(query);

        if (list == null) {
            return ans;
        } else {
            for (WorkTicket workTicket : list){
                String templateName = "作业票类型已删除";
                Long templateId = workTicket.getTicketType();
                WorkTicketTemplate template = workTicketTemplateMapper.selectById(templateId);
                if (template != null) {
                    templateName = template.getName();
                }
                // 缺少判断时间是否成立
                if(!ans.containsKey(templateName)){
                    ans.put(templateName, 1);
                } else {
                    ans.put(templateName, ans.get(templateName) + 1);
                }
            }
            return ans;
        }
    }
}
